
#include "linux_def.h"
#include "core.h"

list_t *list_create(pool_t *pool, int n, size_t size)
{
	list_t	*list;

	list	= palloc(pool, sizeof(list_t));
	if (list == NULL)
		return NULL;

	list->part.elts	= palloc(pool, n * size);
	if (list->part.elts == NULL)
		return NULL;

	list->part.nelts	= 0;
	list->part.next		= NULL;
	list->last		= &list->part;
	list->size		= size;
	list->nalloc		= n;
	list->pool		= pool;

	return list;
}

void *list_push(list_t *l)
{
	void	*elt;
	list_part_t	*last;

	last	= l->last;
	//元素人个数=申请的内存数
	if (last->nelts == l->nalloc){
		//分配一个list_part_t大小给新的数据
		last = palloc(l->pool, sizeof(list_part_t));
		if (last == NULL)
			return NULL;
	
		last->elts = palloc(l->pool, l->nalloc * l->size);
		if (last->elts == NULL)
			return NULL;
		last->nelts	= 0;
		last->next	= NULL;
		
		l->last->next	= last;
		l->last		= last;
	}

	elt	= (char *)last->elts + l->size * last->nelts;
	last->nelts++;

	return elt;
}
